﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.SessionState;
using System.ComponentModel;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Runtime.InteropServices;

namespace LIB
{
    public class MD5Pwd
    {
        [DllImport("MFC13215.DLL", EntryPoint = "Mpk2Dpk")]
        static extern void Mpk2Dpk(byte[] b1, byte[] b2, byte[] b3);

        public static string getMd5Hash(string input)
        {

            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        private static int C2I(char cIn)
        {
            if (cIn >= '0' && cIn <= '9')
                return cIn - '0';
            else if (cIn >= 'A' && cIn <= 'F')
                return cIn - 'A' + 10;
            else
                return 0;
        }
        public static byte[] HexStr2Byte(string strIn)
        {
            int iLen = strIn.Length;
            if (iLen == 0 || iLen % 2 != 0)
                return null;
            byte[] bRet = new byte[iLen / 2];
            for (int i = 0; i < iLen / 2; i++)
            {
                string strTmp = strIn.Substring(i * 2, 2);
                byte bTmp = (byte)(C2I(strTmp[0]) * 16 + C2I(strTmp[1]));
                bRet[i] = bTmp;
            }
            return bRet;
        }
        /// <summary>
        /// 用户卡的加密密钥
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string userMD5(string pwd, string code)//用户卡
        {
            string strOut = getMd5Hash(pwd); //第2步
            //MessageBox.Show( );
            strOut = strOut.ToUpper();
            byte[] mk = HexStr2Byte(strOut); //第3步
            byte[] nsrbh = HexStr2Byte(code);
            byte[] tkey = new byte[100];
            Mpk2Dpk(mk, nsrbh, tkey); //第4步
            string strEnd = "";
            for (int i = 0; i < 16; i++)
            {
                string strTKey = Convert.ToString(tkey[i], 16);
                if (strTKey.Length == 1)
                    strTKey = "0" + strTKey;
                strEnd += strTKey;
            }
            return strEnd.ToUpper();
        }

        /// <summary>
        /// 密码加密
        /// </summary>
        /// <param name="pwd">用户原始密码</param>
        /// <returns>MD5加密后返回字符串的大写</returns>
        public static string EPwd(string pwd)
        {
            string strOut = getMd5Hash(pwd);
            return strOut.ToUpper();
        }
    }
}